<script lang="ts" setup>

  import type { PerFamilyInvestmentApi } from '#/api/per/familyInvestment';
    import { useVbenModal } from '@vben/common-ui';
  import { message, Tabs, Checkbox, Input, Textarea, Select,RadioGroup,CheckboxGroup, DatePicker } from 'ant-design-vue';
    
  import { computed, ref } from 'vue';
  import { $t } from '#/locales';
  import { useVbenForm } from '#/adapter/form';
  import { createFamilyInvestment, getFamilyInvestment, updateFamilyInvestment } from '#/api/per/familyInvestment';

  import { useFormSchema } from '../data';

  const emit = defineEmits(['success']);
  const formData = ref<PerFamilyInvestmentApi.FamilyInvestment>();
  const getTitle = computed(() => {
    return formData.value?.id
            ? $t('ui.actionTitle.edit', ['国境外投资信息'])
            : $t('ui.actionTitle.create', ['国境外投资信息']);
  });

    
  const [Form, formApi] = useVbenForm({
    commonConfig: {
      componentProps: {
        class: 'w-full',
      },
      labelWidth: 80,
    },
      wrapperClass: 'grid-cols-2',
    layout: 'horizontal',
    schema: useFormSchema(),
    showDefaultActions: false
  });

    const [Modal, modalApi] = useVbenModal({
      async onConfirm() {
        const { valid } = await formApi.validate();
        if (!valid) {
          return;
        }
                modalApi.lock();
        // 提交表单
        const data = (await formApi.getValues()) as PerFamilyInvestmentApi.FamilyInvestment;
                try {
          await (formData.value?.id ? updateFamilyInvestment(data) : createFamilyInvestment(data));
          // 关闭并提示
          await modalApi.close();
          emit('success');
          message.success( $t('ui.actionMessage.operationSuccess') );
        } finally {
          modalApi.unlock();
        }
      },
      async onOpenChange(isOpen: boolean) {
        if (!isOpen) {
          formData.value = undefined;
          return;
        }
        // 加载数据
        let data = modalApi.getData<PerFamilyInvestmentApi.FamilyInvestment>();
        if (!data || !data.id) {
          formData.value = data;
          await formApi.setValues(data);
          return;
        }
        if (data.id) {
          modalApi.lock();
          try {
            data = await getFamilyInvestment(data.id);
          } finally {
            modalApi.unlock();
          }
        }
        // 设置到 values
        formData.value = data;
        await formApi.setValues(formData.value);
      },
    });
</script>

<template>
    <Modal :title="getTitle" class="w-[80%]">
      <Form class="mx-4" />
          </Modal>
</template>